﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ev.CaiPiao.Common
{
    public class EmalHelper
    {
        /// <summary>
        /// SMTP实例
        /// </summary>
        static System.Net.Mail.SmtpClient client = null;

        /// <summary>
        /// 发送信息
        /// </summary>
        /// <param name="Receiver">邮件接收人</param>
        /// <param name="Subject">邮件主题</param>
        /// <param name="content">邮件内容</param>    
        public static void SendEmail(string Receiver, string Subject, string content)
        {
            if (string.IsNullOrEmpty(Receiver) || string.IsNullOrEmpty(Subject)
                || string.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException("SendEmail参数空异常！");
            }
            if (client == null)
            {
                try
                {
                    //163发送配置                    
                    client = new System.Net.Mail.SmtpClient();
                    client.Host = "smtp.126.com";
                    client.Port = 25;
                    client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    client.EnableSsl = true;
                    client.UseDefaultCredentials = true;


                    //qq发送配置的参数//切EnableSsl必须设置为true  
                    //client = new System.Net.Mail.SmtpClient();
                    //client.Host = "smtp.qq.com";
                    //client.Port = 25;
                    //client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    //client.EnableSsl = true;
                    //client.UseDefaultCredentials = false;

                    client.Credentials = new System.Net.NetworkCredential("hao77685771991", "wjh577");
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            try
            {
                System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
                Message.SubjectEncoding = System.Text.Encoding.UTF8;
                Message.BodyEncoding = System.Text.Encoding.UTF8;
                Message.Priority = System.Net.Mail.MailPriority.High;

                Message.From = new System.Net.Mail.MailAddress("hao77685771991@163.com", "彩票API");
                //添加邮件接收人地址
                string[] receivers = Receiver.Split(new char[] { ',' });
                Array.ForEach(receivers.ToArray(), ToMail => { Message.To.Add(ToMail); });

                Message.Subject = Subject;
                Message.Body = content;
                Message.IsBodyHtml = true;
                client.Send(Message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
